<template>
  <div class="style-page">
    <div class="head-page-title">{{ showComponentData.title }}</div>
    <div class="line"></div>

    <div class="add-area">
      <p class="add-title">内容样式</p>
      <el-form :model="form" label-width="auto">
        <el-form-item label="页面布局：">
          <el-radio-group v-model="showComponentData.layout">
            <el-radio value="row-2">一行两个</el-radio>
            <el-radio value="left-one-right-two">左一右二</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题文字大小：">
          <div class="flex">
            <el-slider style="width: 68%" v-model="form.titleStyle.fontSize" :min="12" :max="16" />
            <el-input
              type="number"
              style="width: 32%; margin-left: 20px"
              v-model.number="form.titleStyle.fontSize"
              :min="12"
              :max="16"
            >
              <template #append>px</template>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="颜色：">
          <el-color-picker v-model="form.titleStyle.color"></el-color-picker>
          <el-input style="width: 160px; margin-left: 10px" v-model="form.titleStyle.color">
            <template #append>
              <el-button @click="form.titleStyle.color = '#333333'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="字体粗细：">
          <el-radio-group v-model="form.titleStyle.fontWeight">
            <el-radio label="正常" :value="0" />
            <el-radio label="加粗" :value="600" />
          </el-radio-group>
        </el-form-item>

        <el-form-item label="副标题文字大小：">
          <div class="flex">
            <el-slider style="width: 68%" v-model="form.descStyle.fontSize" :min="12" :max="30" />
            <el-input
              type="number"
              style="width: 32%; margin-left: 20px"
              v-model.number="form.descStyle.fontSize"
              :min="12"
              :max="30"
            >
              <template #append>px</template>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="文字颜色：">
          <el-color-picker v-model="form.descStyle.color"></el-color-picker>
          <el-input style="width: 160px; margin-left: 10px" v-model="form.descStyle.color">
            <template #append>
              <el-button @click="form.descStyle.color = '#333333'">重置</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="字体粗细：">
          <el-radio-group v-model="form.descStyle.fontWeight">
            <el-radio label="正常" :value="0" />
            <el-radio label="加粗" :value="600" />
          </el-radio-group>
        </el-form-item>
      </el-form>

      <p class="add-title">添加内容</p>
      <VueDraggable
        v-model="showComponentData.default_sub_list"
        :animation="150"
        ghostClass="ghost"
        :filter="'.el-input'"
        :preventOnFilter="false"
        class="content"
        @end="handleUpdateComponent"
      >
        <div
          class="info-card-item"
          v-for="(item, index) in showComponentData.default_sub_list"
          :key="item.id"
        >
          <el-form label-position="right" label-width="auto">
            <el-form-item label="背景图片：">
              <div class="avatar-uploader" @click="handleChangeImage(index)">
                <el-image v-if="item.imgUrl" :src="item.imgUrl" class="avatar" fit="fill" />
                <i v-else class="el-icon-plus avatar-uploader-icon">
                  <el-icon><Plus /></el-icon>
                </i>
                <div v-if="item.imgUrl" class="mask">替换</div>
                <el-icon
                  v-if="item.imgUrl"
                  class="del-image"
                  :size="20"
                  @click.stop="handleDelImg(index)"
                  ><CircleCloseFilled
                /></el-icon>
              </div>
            </el-form-item>
            <el-form-item label="标题：">
              <el-input v-model="item.title" maxlength="5" show-word-limit />
            </el-form-item>
            <el-form-item label="副标题：">
              <el-input
                v-model="item.desc"
                maxlength="12"
                show-word-limit
                placeholder="副标题内容"
                class="mb10"
              />
            </el-form-item>
<!--            <el-form-item label="绑定数值：">-->
<!--              <el-radio-group v-model="item.total_value">-->
<!--                <el-radio :value="1">顺序练习</el-radio>-->
<!--                <el-radio :value="2">模拟考试</el-radio>-->
<!--                <el-radio :value="3">真实考场</el-radio>-->
<!--                <el-radio :value="4">无</el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
            <el-form-item label="链接：">
              <div class="link">
                <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
                <el-link type="primary" @click="handleOpenLinkDialog(index)">{{
                  item.link?.name || '请选择跳转链接'
                }}</el-link></div
              >
            </el-form-item>
            <el-icon class="del-icon" :size="16" color="red" @click="handleDel(index)">
              <CircleCloseFilled />
            </el-icon>
          </el-form>
        </div>
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          @click="handleAdd"-->
        <!--          plain-->
        <!--          :icon="Plus"-->
        <!--          style="width: 100%"-->
        <!--          v-if="showComponentData.layout == 'row-2'"-->
        <!--        >-->
        <!--          添加卡片-->
        <!--        </el-button>-->
        <el-tag
          type="primary"
          v-if="showComponentData.layout == 'row-2'"
          class="submit-btn"
          @click="handleAdd"
          >添加卡片</el-tag
        >
      </VueDraggable>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :single-type="selectImageSingle"
      :operation-type="false"
      :show-image-choose-dialog="showImageChooseDialog"
      @close-dialog="handleCloseImageDialog"
    ></image-selected-dialog>
  </div>
</template>

<script setup lang="ts">
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import { Plus, CircleCloseFilled, Link } from '@element-plus/icons-vue'
  import debounce from 'lodash/debounce'
  import { reactive, toRaw, watch } from 'vue'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default: () => ({})
    }
  })

  const emit = defineEmits(['updateData'])
  const showComponentData = ref(props.contentData)
  const showLinkDataDialog = ref(false)
  const showImageChooseDialog = ref(false)
  const selectImageSingle = ref(false)
  const changeIndex = ref()

  interface FormData {
    titleStyle: any
    descStyle: any
  }

  const form = reactive<FormData>({
    titleStyle: {
      fontSize: 15,
      color: '#333',
      fontWeight: 600
    },
    descStyle: {
      fontSize: 12,
      color: '#999',
      fontWeight: 0
    }
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.titleStyle = raw.titleStyle
    showComponentData.value.descStyle = raw.descStyle
    handleUpdateComponent()
  }, 300)

  function handleChangeImage(index: number) {
    changeIndex.value = index
    selectImageSingle.value = true
    showImageChooseDialog.value = true
  }

  function handleAdd() {
    showComponentData.value.default_sub_list.push({
      id: Date.now(),
      imgUrl: 'https://image.lutongjiakao.com/system/82895dd1bc8c4960b05f552446bf5c76.png',
      title: '标题',
      desc: '副标题内容'
    })
  }

  function handleCloseImageDialog(value: any) {
    if (value) {
      if (Array.isArray(value)) {
        showComponentData.value.default_sub_list.push(...value)
      } else {
        showComponentData.value.default_sub_list[changeIndex.value].imgUrl = value.fileUrl
      }
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      showComponentData.value.default_sub_list[changeIndex.value].link = value
    }
    showLinkDataDialog.value = false
  }

  function handleOpenLinkDialog(index: number) {
    showLinkDataDialog.value = true
    changeIndex.value = index
  }

  function handleDel(index: number) {
    showComponentData.value.default_sub_list.splice(index, 1)
    handleUpdateComponent()
  }

  function handleDelImg(index: number) {
    showComponentData.value.default_sub_list[index].imgUrl = ''
    handleUpdateComponent()
  }

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }

  onMounted(() => {
    if (showComponentData.value.titleStyle.fontSize) {
      form.titleStyle.fontSize = showComponentData.value.titleStyle.fontSize
      form.titleStyle.fontWeight = showComponentData.value.titleStyle.fontWeight
      form.titleStyle.color = showComponentData.value.titleStyle.color
    }
    if (showComponentData.value.descStyle.fontSize) {
      form.descStyle.fontSize = showComponentData.value.descStyle.fontSize
      form.descStyle.fontWeight = showComponentData.value.descStyle.fontWeight
      form.descStyle.color = showComponentData.value.descStyle.color
    }
  })
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      padding-top: 10px;
      padding-bottom: 20px;
      font-size: 18px;
      font-weight: bold;
      color: black;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .add-area {
      .add-title {
        font-size: 14px;
        font-weight: bold;
        color: black;
        margin: 20px 0;
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }

      .content {
        .info-card-item {
          padding: 10px;
          border-radius: 6px;
          margin-bottom: 12px;
          position: relative;
          background-color: #f7f8fa;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .avatar-uploader {
            width: 60px;
            height: 60px;
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            overflow: hidden;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;

            .mask {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              height: 24px;
              background-color: rgba(0, 0, 0, 0.6);
              display: flex;
              align-items: center;
              justify-content: center;
              opacity: 0;
              transition: opacity 0.3s ease;
              color: #fff;
              font-size: 12px;
            }

            .del-image {
              display: none;
              position: absolute;
              top: 0;
              right: 0;
              cursor: pointer;
              color: #8c939d;
            }

            &:hover .mask {
              opacity: 1;
            }

            &:hover .del-image {
              display: block;
            }

            .avatar {
              width: 100%;
              height: 100%;
              object-fit: cover;
            }

            .avatar-uploader-icon {
              font-size: 28px;
              color: #aaa;
            }
          }

          .link {
            display: flex;
            align-items: center;
            width: 220px;
            height: 40px;
            border-radius: 6px;
            background-color: #fff;
            padding-left: 10px;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }
        }
      }
    }

    .component-style {
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 14px;
          font-weight: bold;
          color: black;
        }
      }
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
    }
  }
</style>
